Method Resolution Order¶

MRO stands for Method Resolution Order. It says that whenever there is multiple inheritance of 2 or more parent classes by one base class, unlike c++, in case of an overloading, the methods and constructors are called in order of left to right .

In [1]:
class A:
    #Python variant of function object of c++
    def __call__(self, x):
        return "Value passed from class-A = "+str(x)
    def __init__(self):
        print("Constructor of class A is called")

class B:
    def __call__(self, x):
        return "Value passed from class-B = "+str(x)
    def __init__(self):
        print("Constructor of class B is called")

Driver code-1¶

In [2]:
if __name__=="__main__":
    class C(A,B):
        pass
    a = C()
    print(a("Hello"))
Constructor of class A is called
Value passed from class-A = Hello

Driver code-2¶

In [3]:
if __name__=="__main__":
    class D(B,A):
        pass
    b = D()
    print(b("World"))
Constructor of class B is called
Value passed from class-B = World